package com.sakura.mall.product.category.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.category.domain.entity.CategoryEntity;
import com.sakura.mall.product.category.domain.request.AddCategoryRequest;
import com.sakura.mall.product.category.domain.request.UpdateCategoryRequest;
import com.sakura.mall.product.category.domain.request.CategoryPageRequest;
import com.sakura.mall.product.category.domain.vo.CategoryVo;
import com.sakura.mall.product.category.mapper.CategoryMapper;
import com.sakura.mall.product.category.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品三级分类 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCategoryRequest request) {
        // todo 其他校验逻辑
        CategoryEntity category = request.convert(CategoryEntity.class);
        this.save(category);
    }

    @Override
    public Page<CategoryVo> queryPage(CategoryPageRequest request) {
        IPage<CategoryEntity> qp = request.toPage();
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CategoryVo> ret = qp.convert(entity -> entity.convert(CategoryVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCategoryRequest request) {
        CategoryEntity entity = request.convert(CategoryEntity.class);
        entity.setCatId(id);
        this.updateById(entity);
    }

}

